package com.feicheng.persistence.service.impl;

import com.feicheng.common.result.PageResult;
import com.feicheng.common.result.ProductQuery;
import com.feicheng.persistence.entity.AProduct;
import com.feicheng.persistence.entity.AStore;
import com.feicheng.persistence.mapper.AProductMapper;
import com.feicheng.persistence.mapper.AStoreMapper;
import com.feicheng.persistence.service.IAProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凯
 * @since 2025-02-21
 */
@Service
public class AProductServiceImpl extends ServiceImpl<AProductMapper, AProduct> implements IAProductService {

    AProductMapper productMapper;
    AStoreMapper storeMapper;
    @Override
    public PageResult<AProduct> getProductPage(ProductQuery query) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", query.getName());
        params.put("statu", query.getStatu());
        params.put("offset", (query.getPage() - 1) * query.getSize());
        params.put("size", query.getSize());

        List<AProduct> list = productMapper.selectProductPage(params);
        int total = productMapper.countProduct(params);

        return new PageResult<>(total, list);
    }
    @Override
    public void saveProduct(AProduct product) {
        if (product.getProductid() == null) {
            product.setProductid(UUID.randomUUID().toString());
            productMapper.insertProduct(product);
        } else {
            productMapper.updateProduct(product);
        }
    }
    @Override
    public void deleteProduct(String productid) {
        productMapper.deleteProduct(productid);
    }
    @Override
    public AProduct getProductById(String productid) {
        return productMapper.selectProductById(productid);
    }
    @Override
    public List<AStore> getStoreList() {
        return storeMapper.selectStoreList();
    }
}
